package com.kdtech.analyse.outside;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;

import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;

/**
 * @author KK
 *
 */
public class TwitterAnalyse implements AnalyseNews {

	private static final String[] regex={
		"https://twitter.com/.*/status/[0-9]{10,}",
		"https://.*.twitter.com/.*/status/[0-9]{10,}",
	};

	
	public boolean isDetailPage(String url) {
		return RegexUtils.matchAnyIgnoreCase(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();

		news.setUrl(url);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
//		System.out.println(html);
		title =doc.select("p.tweet-text").text();
		if(StringUtils.isBlank(title)){
			title =doc.select(".TweetDetail-text").text();
		}
		if(title.length()>80){
			title = title.substring(0, 80)+"...";
		}
		if(StringUtils.isBlank(title)){
			news.setFixTitle(false);
		}
		try {
			date = NumberUtils.parseLong(doc.select("._timestamp[data-time]").attr("data-time"));
			date = date*1000;
		} catch (Exception e) {
		}
		if(date==null){
			date= DateUtils.matchDate(doc.select(".TweetDetail-timeAndGeo").text());
			if(date!=null) date += 1000*60*60*15;
		}
		author= doc.select(".permalink-header  .username  ").text();
		if(StringUtils.isBlank(author)){
			author= doc.select(".TweetDetail-userCell .UserNames-screenName").text();
		}
		content=HtmlCleaner.getContentHtml(url,doc.select(".tweet-text,.tweet-details-fixer"));
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select(".TweetDetail-text,.TweetDetail-media"));
		}
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);

		return news;
	}

	

	

	
}
